<?php

namespace app\modules\Product\models;

use Yii;
use PhpOffice\PhpWord\PhpWord;
 
class Word extends \yii\db\ActiveRecord
{

    public function Line($model){
        if(empty($model->go_time) || empty($model->return_time)){
            echo "行程信息不完整";
            return false; 
        }
        $phpWord = new PhpWord();

        $section = $phpWord->addSection(); // 创建一个区域
        $header = $section->addHeader();
        

        
        $header->addImage(sprintf("%s/web/img/word/%s.png", Yii::$app->basePath, $model->cruise->supplier_id), [
            'positioning'   => 'relative',
            'marginTop'     => -1,
            'marginLeft'    => 1,
            'height'        => 50,
            'wrappingStyle' => 'tight',]);
        $header->addText($model->cruise->name, ['size'=>12, 'bold'=>true],['align'=>'center']);
        $header->addTextBreak(1); // 添加换行符
        $header->addLine([
                'width' => 600,
                'height' => 0,
                'positioning' => 'absolute',
                'color' => 'black',
                'weight' => 1,
        ]);       
        $section->addTextBreak(1); // 添加换行符
        // 标题
        $phpWord->addTitleStyle(1, ['size'=>20, 'bold'=>true, 'color'=>'337ab7',  'numLevel'=>1], ['alignment'=>'center']);
        $phpWord->addTitleStyle(2, ['size'=>14, 'bold'=>true, 'color'=>'337ab7',  'numLevel'=>2], ['alignment'=>'left']);
        $phpWord->addTitleStyle(3, ['size'=>14, 'bold'=>true, 'color'=>'FF0005',  'numLevel'=>2], ['alignment'=>'left']);

        $section->addTitle($model->name, 1);

        $section->addTextBreak(1); // 添加换行符
        $section->addText($model->cruise->desc, ['Inden'=>1000]);
        $section->addTextBreak(1); // 添加换行符

        $section->addTitle(sprintf("行程 (%s - %s)", $model->go_time, $model->return_time),2);

        $tableStyle = ['width' => 50 * 50, 'unit' => 'pct', 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,'borderSize'=>10,'cellMargin'=>100,];

        $firstRowStyle = ['bold' => true];

        $phpWord->addTableStyle('myTable', $tableStyle, $firstRowStyle);
        $table = $section->addTable('myTable');
        //行程
        $row = $table->addRow();
        $cell_style = ['valign'=>'center','bgColor'=>'337ab7'];
        $cellStyle = ['valign'=>'center','alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,];
        $font_style = ['bold'=>true,'color'=>'FFFFFF'];
        $fontStyle = ['bold'=>false];
        $paragraph_style = ['align'=>'center'];
        $price_style = ['color'=>'FF0005'];

        $cell = $table->addCell(2000, $cell_style)->addText('日期', $font_style,$paragraph_style);
        $cell = $table->addCell(6000, $cell_style)->addText('行程', $font_style,$paragraph_style);
        $cell = $table->addCell(1000, $cell_style)->addText('餐饮', $font_style,$paragraph_style);
        $cell = $table->addCell(1000, $cell_style)->addText('住宿', $font_style,$paragraph_style);
       
        $trip = json_decode($model->trip, true);
        ksort($trip);   
        $dates = Yii::$app->dates->prDates($model->go_time, $model->return_time);
        foreach($trip as $key=>$val){
            $table->addRow();
            $item2 = sprintf("%s %s ", $val['title'], $val['desc']);
            $cell = $table->addCell(2000, $cellStyle);
            $cell->addText(sprintf("第%s天", $key+1),$fontStyle,$paragraph_style);
            $cell->addText($dates[$key],$fontStyle,$paragraph_style);

            $cell = $table->addCell(6000,$cellStyle);
            $cell->addText($val['title'],['bold'=>true],$paragraph_style);
            $cell->addText($val['desc'],$fontStyle,$paragraph_style);
            $cell = $table->addCell(1000, $cellStyle)->addText($val['eat'],$fontStyle,$paragraph_style);
            $cell = $table->addCell(1000, $cellStyle)->addText($val['room'],$fontStyle,$paragraph_style);
        }

        $section->addTextBreak(3); // 添加换行符

        // 价格表
        $section->addTitle($model->code. '游轮船舱销售价格:',2);
        $table = $section->addTable('myTable');
        if($model->type == 1){ 
            $row = $table->addRow(400);
            $cellStyle = ['valign'=>'center','bgColor'=>'F4FAFC'];
            $row->addCell(5000, ['gridSpan' => 2, 'valign' => 'center','bgColor'=>'337ab7'])->addText('舱型', $font_style,$paragraph_style);
            $row->addCell(2000, $cell_style)->addText('楼层', $font_style,$paragraph_style);
            $row->addCell(1500, $cell_style)->addText('外卖价/人', $font_style,$paragraph_style);
            $row->addCell(1500, $cell_style)->addText('同行价/人', $font_style,$paragraph_style);

            $room = [];
            foreach($model->productRoom as $val){
                $room[$val->room->type][] = $val;
            }

            foreach($room as $t=>$r){
                $type = \Yii::$app->params['room_type'][$t];
                $row = $table->addRow(); // 行
                $row->addCell(1000, ['vMerge' =>'restart','bgColor'=>'F4FAFC','valign'=>'center'])->addText($type,$fontStyle,$paragraph_style);

                $ids = yii\helpers\ArrayHelper::map($r, 'id', 'id');
                $data = ProductRoomPrice::find()->where(['product_room_id'=>$ids, 'show'=>1])->all();
                if(isset($data[0]) && !empty($data[0])){
                    $obj = $data[0];
                    $ceil = yii::$app->ceils->getCeils($obj->productRoom->room->ceil);
                    $name = sprintf("%s%s %s人房",$obj->productRoom->room->name, $obj->productRoom->room->code, $obj->people);
                    $row->addCell(4000,$cellStyle)->addText($name,$fontStyle,$paragraph_style);
                    $row->addCell(2000,$cellStyle)->addText($ceil,$fontStyle,$paragraph_style);
                    $row->addCell(1500,$cellStyle)->addText($obj->price, $price_style,$paragraph_style);
                    $row->addCell(1500,$cellStyle)->addText($obj->inside_price, $price_style,$paragraph_style);
                    unset($data[0]);
                    $countRoom = count($data);
                    foreach($data as $val){
                        $row2 = $table->addRow(); // 行
                        $row2->addCell(1000, ['vMerge' =>'continue','bgColor'=>'F4FAFC','valign'=>'center']);
                        $name  = sprintf("%s%s %s人房",$val->productRoom->room->name, $val->productRoom->room->code, $val->people);
                        $ceil = yii::$app->ceils->getCeils($val->productRoom->room->ceil);
                        $row2->addCell(4000,$cellStyle)->addText($name,$fontStyle,$paragraph_style);
                        $row2->addCell(2000,$cellStyle)->addText($ceil,$fontStyle,$paragraph_style);
                        $row2->addCell(1500,$cellStyle)->addText($val->price, $price_style,$paragraph_style);
                        $row2->addCell(1500,$cellStyle)->addText($val->inside_price, $price_style,$paragraph_style);
                    }
                }
            }
        }elseif($model->type == 2){
            $row = $table->addRow(400);
            $cellStyle = ['valign'=>'center','bgColor'=>'F4FAFC'];
            $row->addCell(1500, ['valign' => 'center','bgColor'=>'337ab7'])->addText('类别', $font_style,$paragraph_style);
            $row->addCell(1500, $cell_style)->addText('第1/2人价格', $font_style,$paragraph_style);
            $row->addCell(1500, $cell_style)->addText('第1/2人同业价格', $font_style,$paragraph_style);
            $row->addCell(1500, $cell_style)->addText('第3/4人价格', $font_style,$paragraph_style);
            $row->addCell(1500, $cell_style)->addText('第3/4人同业价格', $font_style,$paragraph_style);
            $row->addCell(1000, $cell_style)->addText('单间差', $font_style,$paragraph_style);
            $row->addCell(1500, $cell_style)->addText('备注', $font_style,$paragraph_style);
            if(!empty($model->rooms)){
                $data_rooms = json_decode($model->rooms,true);
                if(is_array($data_rooms)){
                    foreach($data_rooms as $val){
                        $row2 = $table->addRow(); // 行
                        $row2->addCell(1500,$cellStyle)->addText((!empty($val['name']))?$val['name']:"—",$fontStyle,$paragraph_style);
                        $row2->addCell(1500,$cellStyle)->addText((!empty($val['price']))?$val['price']:"—",$fontStyle,$paragraph_style);
                        $row2->addCell(1500,$cellStyle)->addText((!empty($val['inside_price']))?$val['inside_price']:"—", $price_style,$paragraph_style);
                        $row2->addCell(1500,$cellStyle)->addText((!empty($val['price2']))?$val['price2']:"—", $price_style,$paragraph_style);
                        $row2->addCell(1500,$cellStyle)->addText((!empty($val['inside_price2']))?$val['inside_price2']:"—", $price_style,$paragraph_style);
                        $row2->addCell(1000,$cellStyle)->addText((!empty($val['difference']))?$val['difference']:"—", $price_style,$paragraph_style);
                        $row2->addCell(1500,$cellStyle)->addText((!empty($val['remark']))?$val['remark']:"—", $price_style,$paragraph_style);
                    }
                } 
            }

        }    

        /**  费用包含  ***/
        if(!empty($model->cost)){
            $section->addTextBreak(3); 
            $section->addTitle("费用包含 :",3);
            $section->addTextBreak();
            //$html = preg_replace("/style=.+?['|\"]/i",'',$model->cost);//去除样式
            //$html = preg_replace("/rel=.+?['|\"]/i",'',$html);//去除样式
            \PhpOffice\PhpWord\Shared\Html::addHtml($section, $model->cost);
        }
        /**  费用不包含  ***/
        if(!empty($model->nocost)){
            $section->addTextBreak(3); 
            $section->addTitle("费用不包含 :",3);
            $section->addTextBreak();
            // $html = preg_replace("/style=.+?['|\"]/i",'',$model->nocost);//去除样式
            // $html = preg_replace("/rel=.+?['|\"]/i",'',$html);//去除样式
            \PhpOffice\PhpWord\Shared\Html::addHtml($section, $model->nocost);
        }
         /**  取消政策  ***/
        if(!empty($model->cancel)){
            $section->addTextBreak(3); 
            $section->addTitle("取消政策 :",3);
            $section->addTextBreak();
            // $html = preg_replace("/style=.+?['|\"]/i",'',$model->cancel);//去除样式
            // $html = preg_replace("/rel=.+?['|\"]/i",'',$html);//去除样式
            \PhpOffice\PhpWord\Shared\Html::addHtml($section, $model->cancel);
        }
         /**  优惠说明  ***/
        if(!empty($model->discount)){
            $section->addTextBreak(3); 
            $section->addTitle("优惠说明 :",3);
            $section->addTextBreak();
            // $html = preg_replace("/style=.+?['|\"]/i",'',$model->discount);//去除样式
            // $html = preg_replace("/rel=.+?['|\"]/i",'',$html);//去除样式
            \PhpOffice\PhpWord\Shared\Html::addHtml($section, $model->discount);
        }
        /**  签证说明  ***/
        if(!empty($model->visa)){
            $section->addTextBreak(3); 
            $section->addTitle("签证说明 :",3);
            $section->addTextBreak();
            // $html = preg_replace("/style=.+?['|\"]/i",'',$model->visa);//去除样式
            // $html = preg_replace("/rel=.+?['|\"]/i",'',$html);//去除样式
            \PhpOffice\PhpWord\Shared\Html::addHtml($section,$model->visa);
        }
        /**  报名须知  ***/
        if(!empty($model->signup)){
            $section->addTextBreak(3); 
            $section->addTitle("报名须知 :",3);
            $section->addTextBreak();
            // $html = preg_replace("/style=.+?['|\"]/i",'',$model->signup);//去除样式
            // $html = preg_replace("/rel=.+?['|\"]/i",'',$html);//去除样式
            \PhpOffice\PhpWord\Shared\Html::addHtml($section, $model->signup);
        }
        /**  岸上游  ***/
        if(!empty($model->land)){
            $section->addTextBreak(3); 
            $section->addTitle("岸上游 :",3);
            $section->addTextBreak();
            // $html = preg_replace("/style=.+?['|\"]/i",'',$model->land);//去除样式
            // $html = preg_replace("/rel=.+?['|\"]/i",'',$html);//去除样式
            \PhpOffice\PhpWord\Shared\Html::addHtml($section,$model->land);
        }
        /**  自费项目  ***/
        if(!empty($model->expense)){
            $section->addTextBreak(3); 
            $section->addTitle("自费项目 :",3);
            $section->addTextBreak();
            // $html = preg_replace("/style=.+?['|\"]/i",'',$model->expense);//去除样式
            // $html = preg_replace("/rel=.+?['|\"]/i",'',$html);//去除样式
            \PhpOffice\PhpWord\Shared\Html::addHtml($section, $model->expense);
        }
       

        // $cost = str_replace('table', 'Table', $model->cost); // 替换标签
        // \PhpOffice\PhpWord\Shared\Html::addHtml($section, $cost);

        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
		
		$file_name = uniqid();
        $file = sprintf("%s/web/uploads/%s.docx", Yii::$app->basePath, $file_name);
       
        $ret = $objWriter->save($file);
        return $file_name;
    }






}
